package com.qcby.chrom;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class test {
    public static String province = "河北";
    public static String city = "保定";
    //声明需要爬取的网址
    static String URL="https://lishi.tianqi.com/monthdata/baoding/201911";
    //主函数入口
    public static void main(String args[]) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
//        HttpGet httpGet = new HttpGet(URL);
        HttpPost httpGet = new HttpPost(URL);
        // 设置请求标头
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0");
        httpGet.setHeader(":authority","lishi.tianqi.com");
        httpGet.setHeader(":method","POST");
        httpGet.setHeader(":path","/monthdata/baoding/201911");
        httpGet.setHeader(":scheme", "https");
        httpGet.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
        httpGet.setHeader("accept-encoding", "gzip, deflate, br, zstd");
        httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpGet.setHeader("Cookie", "UserId=17537593134677956; Hm_lvt_30606b57e40fddacb2c26d2b789efbcb=1753759314,1753801825; Hm_lpvt_30606b57e40fddacb2c26d2b789efbcb=1753801825; HMACCOUNT=3D8EC4E9711A80E3; Hm_lvt_5326a74bb3e3143580750a123a85e7a1=1753759314,1753801825; Hm_lvt_7c50c7060f1f743bccf8c150a646e90a=1753759314,1753801825; Hm_lpvt_5326a74bb3e3143580750a123a85e7a1=1753803140; Hm_lpvt_7c50c7060f1f743bccf8c150a646e90a=1753803140");
        // 可以继续添加其他需要的标头

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();

            // 也可以获取和打印响应标头
            Header[] headers = response.getAllHeaders();
            for (Header header : headers) {
                System.out.println(header.getName() + ": " + header.getValue());
            }

            if (entity != null) {
                System.out.println(EntityUtils.toString(entity));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}